package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.utils.DateUtils;
import com.weishi.project.api.ret.AnnualPlanRet;
import com.weishi.project.api.vo.DictVo;
import com.weishi.project.sport.home.vo.AnnualPlanRetVO;
import com.weishi.project.training.domain.AnnualPlan;
import com.weishi.project.training.domain.TrainingGroupChannel;
import com.weishi.project.training.mapper.AnnualPlanMapper;
import com.weishi.project.training.mapper.TrainingWeekMapper;
import com.weishi.project.training.service.IAnnualPlanService;
import com.weishi.project.training.service.ITrainingWeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * annualPlanService业务层处理
 */
@Service
public class AnnualPlanServiceImpl extends ServiceImpl<AnnualPlanMapper, AnnualPlan> implements IAnnualPlanService {

    @Autowired
    private AnnualPlanMapper annualPlanMapper;

    @Autowired
    private TrainingWeekMapper trainingWeekMapper;

    @Autowired
    private ITrainingWeekService trainingWeekService;

    /**
     * 查询annualPlan列表
     */
    @Override
    public List<AnnualPlan> selectAnnualPlanList(AnnualPlan annualPlan) {
        return annualPlanMapper.selectAnnualPlanList(annualPlan);
    }

    @Override
    public AnnualPlan getAnnualPlanById(int id) {
        return annualPlanMapper.getAnnualPlanById(id);
    }

    @Override
    public void updateAnnualPlanById(AnnualPlan annualPlan) {
        annualPlanMapper.updateAnnualPlanById(annualPlan);
    }

    @Override
    public List<AnnualPlanRet> selectAnnualPlanRetListByNaturalYearId(int naturalYearId, boolean weekRetListDate) {
        List<AnnualPlanRet> annualPlanRets = new ArrayList<>();

        AnnualPlan temp = new AnnualPlan();
        temp.setNaturalYearId(naturalYearId);
        List<AnnualPlan> dblist = annualPlanMapper.selectAnnualPlanList(temp);

        for(AnnualPlan annualPlan : dblist){
            AnnualPlanRet annualPlanRet = new AnnualPlanRet(annualPlan);
            //if(!weekRetListDate) annualPlanRet.setWeekRets(getWeeks(annualPlan.getStartTime(),annualPlan.getEndTime()));
            if(weekRetListDate)  annualPlanRet.setTrainingWeeks(trainingWeekMapper.getWeekByAnnualPlanId(annualPlan.getId()));
            annualPlanRets.add(annualPlanRet);
        }

        return annualPlanRets;
    }

    /**
     * 重新封装返回值
     */
    @Override
    public List<AnnualPlanRetVO> selectAnnualPlanRetListByNaturalYearIdVO(int naturalYearId, boolean weekRetListDate) {
        List<AnnualPlanRetVO> annualPlanRets = new ArrayList<>();

        AnnualPlan temp = new AnnualPlan();
        temp.setNaturalYearId(naturalYearId);
        List<AnnualPlan> dblist = annualPlanMapper.selectAnnualPlanList(temp);

        for(AnnualPlan annualPlan : dblist){
            AnnualPlanRetVO annualPlanRet = new AnnualPlanRetVO(annualPlan);
            //if(!weekRetListDate) annualPlanRet.setWeekRets(getWeeks(annualPlan.getStartTime(),annualPlan.getEndTime()));
            if(weekRetListDate)  annualPlanRet.setTrainingWeeks(trainingWeekMapper.getWeekByAnnualPlanIdVO(annualPlan.getId()));
            annualPlanRets.add(annualPlanRet);
        }

        return annualPlanRets;
    }

    @Override
    public List<DictVo> getDictVoOneList() {
        return annualPlanMapper.getDictVoOneList();
    }

    @Override
    public List<DictVo> getDictVoTwoList(String dictType) {
        return annualPlanMapper.getDictVoTwoList(dictType);
    }

    @Override
    public List<TrainingGroupChannel> getAchievementList() {
        return annualPlanMapper.getAchievementList();
    }

    @Override
    public void updateAchievementMs(Map<String, Object> params) {
        annualPlanMapper.updateAchievementMs(params);
    }

    private static List<AnnualPlanRet.WeekRet> getWeeks(Date start, Date end){
        List<AnnualPlanRet.WeekRet> weekRets = new ArrayList<>();
        Date startTemp = start;
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTemp);
        int w=cal.get(Calendar.DAY_OF_WEEK)-1;
        if(w==0) w=7;
        Date endTemp = DateUtils.getSpecifiedDayAfter(startTemp,7-w);
        int index = 1;
        boolean flag = true;
        while (flag){
            AnnualPlanRet.WeekRet weekRet = AnnualPlanRet.getWeekRet();
            weekRet.setStartTime(startTemp);
            weekRet.setIndex(index++);
            if(endTemp.after(end)){
                endTemp = end;
                flag = false;
            }
            weekRet.setEndTime(endTemp);
            weekRets.add(weekRet);
            startTemp = DateUtils.getSpecifiedDayAfter(endTemp,1);
            endTemp = DateUtils.getSpecifiedDayAfter(startTemp,6);
        }
        return weekRets;
    }

}